{% extends 'base.html' %}
{% load static %}


{% block head %}
<script>

	//js前台更新购物车数据
	function update_page_info(){
		total_count = 0
		total_price = 0
		$(".cart_list_td").find(":checked").parents("ul").each(function(){
			count = $(this).find(".num_show").val()
			amount = $(this).children(".col07").text()
			count = parseInt(count)
			amount = parseInt(amount)

			total_count += count
			total_price += amount
		})
		console.log(total_price)
		$(".settlements").find("b").text(total_count)
		$(".settlements").find("em").text(total_price.toFixed(2))
	}






</script>
{% endblock head %}


{% block center_body %}
	<div class='total_count'>全部商品<em>{{ total_count }}</em>件</div>
	<ul class='cart_list_th clearfix'>
		<li class='col01'>商品名称</li>
		<li class='col02'>商品单位</li>
		<li class='col03'>商品单价</li>
		<li class='col04'>数量</li>
		<li class='col05'>小计</li>
		<li class='col06'>操作</li>
	</ul>

	<form method="post" action="/order/place">
		{% csrf_token %}
    {% for sku in goods %}
	<ul class='cart_list_td clearfix' id='{{ sku.id }}'>
		<li class='col01' id="check_goods_id"><input type='checkbox' name='sku_ids' value="{{ sku.id }}" id='check_goods' checked='checked'></li>
		<li class='col02'><img src='/static/{{ sku.image }}'></li>
		<li class='col03'>{{ sku.name }}<br><em>{{ sku.price }}元/{{ sku.unit }}</em><br><em>库存：<span id='goods_kucun'>{{ sku.stock }}</span></em></li>
		<li class='col04'>{{ sku.unit }}</li>
		<li class='col05'>{{ sku.price }}</li>
		<li class='col06'>
			<div class='num_add'>
				<a href='javascript:;' class='add fl'>+</a>
				<input type='text' class='num_show fl' value='{{ sku.count }}' kucun='{{ sku.kucun }}'>
				<a href='javascript:;' class='minus fl'>-</a>
			</div>
		</li>
		<li class='col07'>{{ sku.amount }}</li>
		<li class='col08'><a href='javascript:cart_del({{ sku.id }});'>删除</a></li>
	</ul>
    {% endfor %}

	<ul class='settlements'>
		<li class='col01'><input type='checkbox' name='' checked='' id='check_all' href='javascript:;'></li>
		<li class='col02'>全选</li>
		<li class='col03'>合计(不含运费)：<span>¥</span><em id='total'>{{ total_price }}</em><br>共计<b class='total_count1'>{{ total_count }}</b>件商品</li>
		{#<li class='col04'><a href='javascript:go_order()'>去结算</a></li>#}
		<li class='col04'><input type="submit"  value="去结算"> </li>
	</ul>
		</form>
<script>
	function update_page_info(){
		total_count = 0
		total_price = 0
		$(".cart_list_td").find(":checked").parents("ul").each(function(){
			count = $(this).find(".num_show").val()
			amount = $(this).children(".col07").text()
			count = parseInt(count)
			amount = parseInt(amount)

			total_count += count
			total_price += amount
		})
		console.log(total_price)
		$(".settlements").find("b").text(total_count)
		$(".settlements").find("em").text(total_price.toFixed(2))
	}


	//js部分选中时，更新数据
	$('.cart_list_td').find(':checkbox').change(function(){
		console.log(1)
		all_len = $('.cart_list_td').length
		check_length = $('.settlements').find(':checked').length
		console.log(all_len,check_length)
		is_checked = true
		if (check_length < all_len){
				is_checked = false
		}
		$('.settlements').find(':checkbox').prop('checked',is_checked)


		update_page_info()
	})


	//js全选，全不选
	$(".settlements").find(":checkbox").change(function (){
		is_checked_all = $(this).prop("checked")
		console.log('---',is_checked_all)
		$(".cart_list_td").find(":checkbox").each(function(){
			$(this).prop('checked', is_checked_all)
		})

		update_page_info()
	})

	//修改购物车数量
	$('.add').

</script>
{% endblock center_body %}

